<template>
	<view class="page" :style="'height:' + height + 'rpx'">
		<view class="noDataTip" v-if="result.length == 0">
			<view class="iconfont icon-wuanpai" style="color:#6B6B6B;margin-right: 25rpx;font-size: 45rpx;"></view>
			<view style="font-size: 45rpx;">暂无浏览记录!</view>
		</view>
		<view class="result" v-else>
			<view class="button_top" >
				<view  class="delete" v-if="!choose">
					<button class="delete_button" @click="choose = true">
						<text >删除记录</text>
					</button>
				</view>
				<view  class="delete" v-else>
					<button class="delete_button" @click="deleteData()">
						<text >确定</text>
					</button>
					<button  class="cancel" @click="choose = false">
						<text>取消</text>
					</button>
				</view>

				<view class="tip">删除后，会同时消除对方的来访记录</view>
			</view>
			<checkbox-group class="goodsContent" @change="checkboxChange">
				<view class="date_item" v-for="(item,index) in result" :key="index">
					<view class="top">
						{{item.date}}
					</view>
					<label class="goodsCard" v-for="(item1,index1) in item.esvoList" :key="index1">
						<view class="checkbox" v-if="choose">
							<checkbox :value="JSON.stringify(item1)" color="#23b8ff" />
						</view>
						<miTaPersonItem :person="item1" :lineHeight="false" class="goodsData"
							@toHimCourse="toHimCourse()">
							<!-- 							<template v-slot:personName>
								<view class="person_name" v-html="item.esobject.highLight.name"></view>
							</template> -->
						</miTaPersonItem>
					</label>
					<!-- 					<miTaPersonItem class="person_item" v-for="(item1,index1) in item.esvoList" :key="index1"
						:person="item1" :lineHeight="false" @toHimCourse="toHimCourse()"></miTaPersonItem> -->
				</view>
			</checkbox-group>
		</view>

	</view>

</template>

<script>
	import miTaPersonItem from "../../components/miTaPersonItem/miTaPersonItem.vue"
	import toHimCourseMixin from "../../static/js/toHimCourse.js"
	export default {
		// mixins: [toHimCourseMixin],
		onLoad() {
			//广告
			if (wx.createRewardedVideoAd) {
				this.rewardedVideoAd = wx.createRewardedVideoAd({
					adUnitId: 'adunit-db19f55e90f0f2c3'
				})
				this.rewardedVideoAd.onLoad(() => {
					// console.log('激励视频 广告加载成功')
				})
				this.rewardedVideoAd.onError((err) => {
					// console.log('拉取失败', err)
				})
				// this.rewardedVideoAd.onClose((res) => {
				// 	// console.log('激励视频 广告关闭');
				// 	if (res.isEnded) {

				// 	} else {
				// 		uni.showToast({
				// 		    title: '未删除成功',
				// 			icon: 'error',
				// 		    duration: 2000
				// 		});
				// 	}

				// })
			}


			uni.showLoading({
				title: '加载中..',
				// mask: true
			});
			this.header = getApp().globalData.header;
			this.getFootData();

			let that = this;
			uni.getSystemInfo({
				success: function(res) {
					let pxToRpxScale = 750 / res.windowWidth;
					// console.log(res.windowHeight);

					that.height = res.windowHeight * pxToRpxScale;
					// console.log(that.height)
				}
			})
		},
		data() {
			return {
				showBotton: false,//要加一个变量，要于最开始请求的时候没数据，但是此时button不可显示，所以不能用result的长度决定
				rewardedVideoAd: null, //广告
				height: 0,
				header: {},
				result: [],
				choose: false,
				delete: [], //要删除的数组
				hasAuthorityToSeeParams: { //访问他人课表的参数
					Authority: "",
					no: ""
				},
				browsingHistory: { //添加浏览记录的参数
					no: ""
				},
			}
		},
		methods: {
			checkboxChange(e) {
				// console.log('选中',e.detail)
				this.delete = [];
				e.detail.value.forEach((item,index) => {
					this.delete.push(JSON.parse(item).source.no)
				})
				console.log(this.delete)
			},
			async getFootData() {
				let res = await getApp().UniRequest("/mita/authority/browsingHistory/mine", "Get", "",
					this.header);
				if (res.code === 20000) {
					// console.log('浏览记录', res.data)
					let data = res.data;
					this.result = data.stu.list;
					if (this.result.length != 0) {
						this.showBotton = true
					}
					console.log(this.result)
				}
				uni.hideLoading();
			},
			deleteData() {
				let that = this;
				// console.log('执行')
				if (this.delete.length == 0) {
					uni.showToast({
						title: '请选择成员',
						icon: 'error',
						duration: 2000
					});
				} else {
					uni.showModal({
						title: '提示',
						content: '确定清除记录?',
						success: async function(res) {
							if (res.confirm) {
								console.log(that.delete)
								that.deleteImp()
					
							} else if (res.cancel) {
								// console.log('用户点击取消');
							}
						}
					});
				}


			},
			async deleteImp() {
				uni.showLoading({
					title: '删除中'
				});
				// let data = {
				// 	hisNos: []
				// }
				// data.hisNos = this.delete;
				console.log(this.delete)
				let res = await getApp().UniRequest("/mita/authority/browsingHistory/one",
					"DELETE", this.delete,
					this.header);
				console.log('删除结果',res)
				if (res.code === 20000) {
					// console.log(res);
					uni.showToast({
						title: '删除成功',
						icon: 'success',
						duration: 2000
					});
					uni.showLoading({
						title: '加载中..',
						// mask: true
					});
					this.choose = false;
					this.getFootData()
				} else {
					uni.showToast({
						title: '请稍后重试',
						icon: 'error',
						duration: 2000
					});
				}
				uni.hideLoading();
			},
			async toHimCourse(item) {
				if (this.choose) {
					
				} else {
					uni.showLoading({
						title: '加载中..',
						mask: true
					});
					let header = new Object()
					header.Authority = uni.getStorageSync("Authority");
					this.hasAuthorityToSeeParams.Authority = header.Authority;
					this.hasAuthorityToSeeParams.no = item.no;
					this.browsingHistory.no = item.no;
					let miTaIsHaiZhu;
					if (item.collegeId == 13 || item.collegeId == 4 || item.collegeId == 11) {
						miTaIsHaiZhu = true
					} else {
						miTaIsHaiZhu = false
					}
					let res = await getApp().UniRequest("/mita/authority/hasAuthorityToSee", "Get", this
						.hasAuthorityToSeeParams,header);
					// console.log(res)
					if (res.code === 20008) {
						uni.showToast({
							title: '对方设置权限，无法访问',
							icon: 'none',
							duration: 3000
						});
						uni.hideLoading()
					}
					if (res.code === 20000) {
						// console.log('111');
						// let data = await getApp().UniRequest("/mita/authority/browsingHistory", "PUT", this
						// 	.browsingHistory,
						// 	header);
						// if (data.code === 20000) {
						// 	// console.log('添加浏览记录成功');
						// } else {
						// 	// console.log('添加浏览记录失败')
						// }
						uni.navigateTo({
							url: '/page_miTa/miTaCourse/miTaCourse?Authority=' + res.data.Authority +
								'&miTaIsHaiZhu=' + miTaIsHaiZhu + '&name=' + item.name + '&no=' + item.no
						});
						uni.hideLoading()
					}
				}
			},
		},
		components: {
			miTaPersonItem
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		position: relative;
		// padding: 20rpx;
		// padding-top: 20rpx;
		padding-bottom: 20rpx;
		background-color: #f2f2f2;

		button {
			background-color: #fff;
			border: 0rpx;
			padding-right: 10rpx;
			padding-left: 10rpx;
		}

		.noDataTip {
			height: 150rpx;
			width: 650rpx;
			line-height: 50rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			position: absolute;
			top: 50%;
			margin-top: -150rpx;
			left: 50%;
			margin-left: -325rpx;
			font-size: 50rpx;
			color: #6B6B6B;
			// background-color: #fff;
			// box-shadow: 0.5rpx 0.5rpx 40rpx #DBDBDB;
			border-radius: 20rpx;
		}

		.result {
			height: 100%;
			width: 100%;
			
			.button_top {
				position: relative;
				height: 100rpx;
				background-color: #f2f2f2;
				.delete {
					display: flex;
					position: absolute;
					// padding-left: 20rpx;
					// padding-right: 20rpx;
					padding: 20rpx;
					top: 20rpx;
					right: 30rpx;
					// height: 70rpx;
					font-size: 30rpx;
					// background-color: #23b8ff;
					
					font-weight: 700;
					// line-height: 70rpx;
					letter-spacing: 3rpx;
					.delete_button {
						background-color: #23b8ff;
						color: #FFFFFF;
						// height: 100%;
					}
					.cancel {
						margin-left: 20rpx;
						color: #737373;
					}
				}

				.tip {
					position: absolute;
					top: 140rpx;
					right: 30rpx;
					font-size: 24rpx;
					color: #858585;
					letter-spacing: 3rpx;
				}
			}

			.goodsContent {
				background-color: #f2f2f2;
				padding-left: 10rpx;
				padding-right: 10rpx;
				margin-top: 100rpx;
				// padding-top: ;
				.date_item {
					position: relative;
					margin: 0 auto;
					margin-top: 70rpx;
					width: 100%;
					// height: 400rpx;
					border-top-left-radius: 10rpx;
					border-top-right-radius: 10rpx;
					border-top: 3rpx solid #7D7D7D;
					background-color: #F7F7F7;

					&:first-child {
						margin-top: 40rpx;
					}

					.top {
						position: absolute;
						top: -30rpx;
						left: 20rpx;
						padding-left: 20rpx;
						padding-right: 20rpx;
						// width: 40%;
						height: 60rpx;
						line-height: 60rpx;
						text-align: center;
						font-size: 30rpx;
						background-color: #f2f2f2;

					}

					.goodsCard {
						display: flex;
						position: relative;
						margin: 0 auto;
						// padding: 0rpx;

						.checkbox {
							width: 5%;
							text-align: center;
							line-height: 200rpx;
							padding-left: 10rpx;
						}

						.goodsData {
							width: 100%;
							height: 100%;
							display: flex;
						}

					}


				}
			}
		}
	}
</style>
